home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / gs261 / ps2image.ps < prev    next >
Text File  |  1995-07-17  |  6KB  |  186 lines

  1. %    Copyright (C) 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2. %
  3. % This file is part of Ghostscript.
  4. %
  5. % Ghostscript is distributed in the hope that it will be useful, but
  6. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. % to anyone for the consequences of using it or for whether it serves any
  8. % particular purpose or works at all, unless he says so in writing.  Refer
  9. % to the Ghostscript General Public License for full details.
  10. %
  11. % Everyone is granted permission to copy, modify and redistribute
  12. % Ghostscript, but only under the conditions described in the Ghostscript
  13. % General Public License.  A copy of this license is supposed to have been
  14. % given to you along with Ghostscript so you can know your rights and
  15. % responsibilities.  It should be in a file named COPYING.  Among other
  16. % things, the copyright notice and this notice must be preserved on all
  17. % copies.
  18.  
  19. % Convert a .ps file to another .ps file containing only a bit image.
  20. % After loading ps2image.ps into Ghostscript, invoke
  21. %    (output_file.ps) ps2image
  22. % This replaces the current device, writing the output on the file
  23. % instead of to the device.  Then invoke
  24. %    (your_input_file.ps) run
  25. % To display the image at a later time, simply run the file that was
  26. % written (output_file.ps).
  27.  
  28. % Initialize, and redefine copypage and showpage.
  29. /ps2idict 25 dict def
  30. ps2idict begin
  31.  
  32.         % Save the showpage operator.
  33.   /realshowpage /showpage load def
  34.  
  35.         % Define a monochrome palette.
  36.   /monopalette <00 ff> def
  37.  
  38.         % Initialize the tables for reading a image.
  39.         % This code gets copied to the output file.
  40.   /maxrep 31 def        % max repeat count
  41.   /maxrep1 maxrep 1 sub def
  42.   /initread
  43.    {
  44.         % Initialize the strings for filling runs.
  45.  
  46.      /.ImageFills [
  47.      0 1 255
  48.       { //maxrep string dup 0 1 //maxrep1 { 3 index put dup } for
  49.         pop exch pop readonly
  50.       } for
  51.      ] readonly def
  52.  
  53.         % Initialize the procedure table for input dispatching.
  54.  
  55.      /.ImageProcs [
  56.      33 { { pop .ImageItem } } repeat
  57.      32 { {    % 0x21-0x40: (N-0x20) data bytes follow
  58.       32 sub 3 index exch 0 exch getinterval 2 index exch
  59.       readhexstring pop exch pop dup
  60.      } bind } repeat
  61.      31 { {    % 0x41-0x5f: repeat last data byte (N-0x40) times
  62.       64 sub .ImageFills 2 index dup length 1 sub get get
  63.       exch 0 exch getinterval
  64.      } bind } repeat
  65.      160 { { pop .ImageItem } } repeat
  66.      ] readonly def
  67.  
  68.    } def
  69.         % Read one item from a compressed image.
  70.         % This procedure gets copied to the output file.
  71.         % Stack contents: <buffer> <file> <previous>
  72.   /.ImageItem
  73.    { 1 index read pop dup .ImageProcs exch get exec
  74.    } def
  75.         % Read and print an entire compressed image,
  76.         % scaling it uniformly in X and Y to fill the page.
  77.         % This procedure gets copied to the output file.
  78.         % Arguments: <width> <height>
  79.   /.ImageRead
  80.    { gsave 1 [
  81.      clippath pathbbox pop pop translate
  82.      pathbbox newpath 4 -2 roll pop pop
  83.      dup 3 1 roll abs 5 index exch div exch abs 6 index exch div
  84.      2 copy lt { exch } if pop        % (definition of max)
  85.      0 0 2 index neg 0 4 index 7 -1 roll mul
  86.      ] { .ImageItem }
  87.      4 index 7 add 8 idiv string currentfile ()
  88.      8 3 roll
  89.      image pop pop pop
  90.      grestore showpage
  91.    } def
  92.  
  93.         % Write a repeat command on the file.
  94.   /writerepeat        % <count> writerepeat -
  95.    {  { dup 31 le { exit } if myfile (_) writestring 31 sub } loop
  96.      dup 0 ne { 64 add myfile exch write } { pop } ifelse
  97.    } bind def
  98.  
  99.         % Write a data command on the file.
  100.   /writedata        % <string> writedata -
  101.    {  { dup length 0 eq { exit } if
  102.     dup length 32 min
  103.     dup 16#20 add myfile exch write
  104.     1 index 0 2 index getinterval myfile exch writehexstring
  105.     myfile (\n) writestring
  106.     1 index length 1 index sub getinterval
  107.       }
  108.      loop pop
  109.    } bind def
  110.  
  111.         % Write an entire data string on the file.
  112.   /writedatastring        % <string> writedatastring -
  113.    {  { dup length 4 lt { exit } if
  114.         % Detect a maximal run of non-repeated data.
  115.     dup length 4 sub 0 exch 1 exch
  116.      { 2 copy 3 getinterval 2 index 2 index 1 add 3 getinterval
  117.        eq { exit } if pop
  118.      }
  119.     for
  120.     dup type /stringtype eq { exit } if    % no repetition found
  121.     1 add
  122.     1 index 0 2 index getinterval writedata
  123.     1 index length 1 index sub getinterval
  124.         % Detect a maximal run of repeated data.
  125.         % We know there are at least 3 repeated bytes.
  126.     dup 0 get exch
  127.     dup length exch 3 1 3 index 1 sub
  128.      {        % Stack: <byte> <length> <string> <index>
  129.        2 copy get 4 index ne { 3 -1 roll pop exch exit } { pop } ifelse
  130.      }
  131.     for        % Stack: <byte> <length> <string>
  132.     exch dup writerepeat
  133.     1 index length 1 index sub getinterval
  134.     exch pop
  135.       }
  136.      loop writedata
  137.    } bind def
  138.  
  139.         % The main procedure.
  140.   /ps2image
  141.    {                % Open the file
  142.      (w) file /myfile exch def
  143.      /initread load
  144.       { dup myfile exch write==only
  145.         type dup /arraytype eq exch /packedarraytype eq or
  146.      { (\n) } { ( ) } ifelse myfile exch writestring
  147.       } forall
  148.      { /.ImageItem /.ImageRead }
  149.       { dup myfile exch write==only
  150.         load myfile exch write==only
  151.     myfile ( bind def\n) writestring
  152.       } forall
  153.                     % Get the device parameters
  154.      currentdevice getdeviceprops .dicttomark
  155.      dup /HWSize get aload pop
  156.        /devheight exch def
  157.        /devwidth exch def
  158.      /InitialMatrix get
  159.        /devmatrix exch def
  160.                 % Make a corresponding memory device
  161.      devmatrix devwidth devheight monopalette
  162.      makeimagedevice
  163.      /mydevice exch def
  164.      mydevice setdevice        % (does an erasepage)
  165.      /rowwidth devwidth 7 add 8 idiv def
  166.      /row rowwidth 7 add 8 idiv 8 mul string def    % pad for scanning
  167.                 % Replace the definition of showpage
  168.      userdict /showpage { ps2idict begin myshowpage end } bind put
  169.    } def
  170.                 % Write the image on the file
  171.   /myshowpage
  172.    { myfile devwidth write==only   myfile ( ) writestring
  173.      myfile devheight write==only   myfile ( .ImageRead\n) writestring
  174.                  % Write the hex data
  175.      0 1 devheight 1 sub
  176.       { mydevice exch row 0 rowwidth getinterval copyscanlines
  177.     writedatastring
  178.       } for
  179.      myfile flushfile
  180.      erasepage initgraphics
  181.    } bind def
  182.  
  183. end
  184.  
  185. /ps2image { ps2idict begin ps2image end } bind def
  186.